﻿using System.Collections.Generic;
using System.Linq.Expressions;
using Web7.Container.Controllers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Web7.Container.Repositories;
using Web7.Container.Services;
using Web7.Container.Models;
using System.Web.Mvc;
using Rhino.Mocks;
using Web7.Container.Services.Impl;
using Web7.Core.Security;

namespace Web7.Container
{
    /// <summary>
    ///这是 UserControllerTest 的测试类，旨在
    ///包含所有 UserControllerTest 单元测试
    ///</summary>
    [TestClass()]
    public class UserControllerTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试特性
        // 
        //编写测试时，还可使用以下特性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        public IUserService UserServiceInstance { get; set; }
        public IRoleService RoleServiceInstance { get; set; }
        public ILocalizationService LocalizationServiceInstance { set; get; }
        public IAssistUserService AssistUserServiceInstance { get; set; }
        public IUserRepository UserRepositoryInstance { get; set; }

        [TestInitialize]
        public void SetUp()
        {
            Guid userGuid = SequentialGuid.NewGuid();
            var user = new User { Id = userGuid, UserName = "zhangsan", Password = "111111", Email = "dingle@we7.cn" };
            var updateUser = new User { Id = userGuid, UserName = "lisi", Password = "111111", Email = "dingle@we7.cn" };

            Guid roleGuid = SequentialGuid.NewGuid();
            var role = new Role { Id = roleGuid, RoleName = "Read", IsSystem = true };
            var updateRole = new Role { Id = roleGuid, RoleName = "Read", IsSystem = true };
            Expression<Func<User,bool>> fucnUser = f => true;


            UserRepositoryInstance = MockRepository.GenerateMock<IUserRepository>();
            UserServiceInstance = MockRepository.GenerateMock<IUserService>();
            RoleServiceInstance = MockRepository.GenerateMock<IRoleService>();
            LocalizationServiceInstance = MockRepository.GenerateMock<ILocalizationService>();
            AssistUserServiceInstance = MockRepository.GenerateMock<IAssistUserService>();

            UserRepositoryInstance.Stub(u => u.Get(userGuid)).Return(user);
            UserRepositoryInstance.Stub(u => u.Get(fucnUser)).Return(user);
            UserRepositoryInstance.Stub(u => u.Get(fucnUser)).Return(user);


            UserRepositoryInstance.Stub(u => u.Update(updateUser));
            UserRepositoryInstance.Stub(u => u.Count(fucnUser));
            UserRepositoryInstance.Stub(u => u.Create(user));
            UserRepositoryInstance.Stub(u => u.Fetch(fucnUser)).Return(new List<User> { user });

            UserServiceInstance = new UserService(UserRepositoryInstance);

            var fuck = UserServiceInstance.Get(fucnUser); //出不来
            fuck = UserServiceInstance.Get(userGuid);   //能出来

            RoleServiceInstance.Stub(r => r.Get(roleGuid)).Return(role);
            RoleServiceInstance.Stub(r => r.Get(f=>true)).Return(role);
            RoleServiceInstance.Stub(u => u.Update(updateRole));
            RoleServiceInstance.Stub(u => u.Count(p => true));
            RoleServiceInstance.Stub(u => u.Create(role));
            RoleServiceInstance.Stub(u => u.Fetch(p=>true)).Return(new List<Role> { role });


            var userparames = new CreateUserParams(user.UserName, user.Password, user.Email, null, null, true);
            AssistUserServiceInstance.Stub(s => s.VerifyUserUnicity("zhangsan", "dingle@we7.cn")).Return(false);
            AssistUserServiceInstance.Stub(s => s.CreateUser(Arg.Is(userparames))).Return(user);
        }

        /// <summary>
        ///CreatePost 的测试
        ///</summary>
        [TestMethod()]
        public void CreatePostTest()
        {
            UserController target = new UserController(UserServiceInstance, LocalizationServiceInstance, RoleServiceInstance) { AssistUserService = AssistUserServiceInstance };

            UserViewModel createModel = new UserViewModel() { UserName = "zhangsan", ConfirmPassword = "111111", Email = "dingle@we7.cn", Password = "111111" };

            ActionResult actual = target.CreatePost(createModel);
            Assert.AreEqual("Index", ((RedirectToRouteResult)(actual)).RouteValues["action"]);
        }
    }
}
